use anyhow::Error;
use mysql_async::{prelude::Queryable, Conn, Row};

use super::Tables;

pub async fn get_all_tables(db: &str, pool: &mut Conn) -> Result<Vec<Tables>, Error> {
    let sql = if db.trim().is_empty() {
        format!("select table_schema,table_name from information_schema.tables where table_type ='BASE TABLE' and engine = 'StarRocks'")
    } else {
        format!("select table_schema,table_name from information_schema.tables where table_type ='BASE TABLE' and table_schema = '{}' and engine = 'StarRocks'", db)
    };
    let ret = pool.query(sql).await?;
    Ok(ret)
}

pub async fn get_tablet_by_name(
    schema: &str,
    name: &str,
    pool: &mut Conn,
) -> Result<Vec<Row>, Error> {
    let sql = format!("show tablet from {}.{}", schema, name);
    let ret = pool.query(sql).await?;
    Ok(ret)
}

pub async fn get_data_size(schema: &str, name: &str, pool: &mut Conn) -> Result<Vec<Row>, Error> {
    let sql = format!("show data from {}.{}", schema, name);
    let ret = pool.query(sql).await?;
    Ok(ret)
}
